package com.hzh.cusviewdemo.AnimationView.Splash;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

import com.hzh.cusviewdemo.R;

/**
 * 自定义旋转聚合欢迎动画
 */
public class SplashView extends View {

    //旋转圆的画笔
    private Paint mPaint;
    //扩散圆的画笔
    private Paint mHolePaint;
    //属性动画
    private ValueAnimator mValueAnimator;

    //背景色
    private int mBackgroundColor = Color.BLACK;
    //颜色数组，代表6个球的颜色
    private int[] mCircleColors = new int[]{Color.RED, Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK, -16777216};

    //表示旋转圆的中心坐标
    private float mCenterX;
    private float mCenterY;
    //表示斜对角线长度的一半,扩散圆最大半径
    private float mDistance;

    //6个小球的半径
    private float mCircleRadius = 18;
    //旋转大圆的半径
    private float mRotateRadius = 90;

    //当前大圆的旋转角度
    private float mCurrentRotateAngle = 0F;
    //当前大圆的半径
    private float mCurrentRotateRadius = mRotateRadius;
    //扩散圆的半径，即水波纹的半径
    private float mCurrentHoleRadius = 0F;
    //表示旋转动画的时长
    private int mRotateDuration = 1200;

    private SplashState mState;


    public SplashView(Context context) {
        super(context);
        init(context);
    }

    public SplashView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SplashView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //开始绘制动画
        if (mState == null) {
            mState = new RotateState();
        }
        mState.drawState(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //获取圆心坐标
        mCenterX = w * 1f / 2;
        mCenterY = h * 1f / 2;
        //斜对角线的长度/2
        mDistance = (float) (Math.hypot(w, h) / 2);
    }

    /**
     * 这个抽象类，对外提供drawState方法，供子类实现
     */
    private abstract class SplashState {
        abstract void drawState(Canvas canvas);
    }

    /**
     * 1.旋转
     * 需要在此绘制6个小球、背景
     */
    private class RotateState extends SplashState {

        private RotateState() {
            //旋转一周，动画从0开始，直到Math.PI*2
            mValueAnimator = ValueAnimator.ofFloat(0, (float) (Math.PI * 2));
            //执行模式，执行2次
            mValueAnimator.setRepeatCount(2);
            //设置动画时长
            mValueAnimator.setDuration(mRotateDuration);
            //设置插值器。默认先加速后减速，这里使用线性插值器。
            mValueAnimator.setInterpolator(new LinearInterpolator());
            //监听动画执行过程
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    //在这里需要得到动画旋转的角度
                    mCurrentRotateAngle = (float) animation.getAnimatedValue();
                    //使onDraw()方法重新被调用
                    invalidate();
                }
            });

            //监听动画的执行状态
            mValueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    //切换到第二种动画
                    mState = new MerginState();
                }
            });
            //使动画执行
            mValueAnimator.start();
        }

        @Override
        void drawState(Canvas canvas) {
            //绘制背景
            drawBackground(canvas);

            //绘制6个小球
            drawCircles(canvas);
        }
    }

    /**
     * 2.扩散聚合
     * 先向外扩散，再聚合
     */
    private class MerginState extends SplashState {

        private MerginState() {
            //扩散的执行半径，从小圆的半径mCircleRadius开始，到大圆mRotateRadius结束
            mValueAnimator = ValueAnimator.ofFloat(mCircleRadius, mRotateRadius);
            //设置动画时长
            mValueAnimator.setDuration(mRotateDuration);
            //设置插值器。反向执行效果。
            mValueAnimator.setInterpolator(new OvershootInterpolator(10f));
            //监听动画执行过程
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    //得到动画执行过程中的半径
                    mCurrentRotateRadius = (float) animation.getAnimatedValue();
                    //使onDraw()方法重新被调用
                    invalidate();
                }
            });

            //监听动画的执行状态
            mValueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    //切换到第二种动画
                    mState = new ExpandState();
                }
            });
            //使动画反向执行
            mValueAnimator.reverse();
        }

        @Override
        void drawState(Canvas canvas) {

            //绘制背景
            drawBackground(canvas);
            //绘制小球
            drawCircles(canvas);
        }
    }

    /**
     * 3. 水波纹
     *
     * @param
     */
    private class ExpandState extends SplashState {

        private ExpandState() {
            //扩散的执行半径，从小圆的半径mCircleRadius开始，到mDistance结束
            mValueAnimator = ValueAnimator.ofFloat(mCircleRadius, mDistance);
            //设置动画时长
            mValueAnimator.setDuration(mRotateDuration);
            //设置插值器。默认先加速后减速，这里使用线性插值器。
            mValueAnimator.setInterpolator(new LinearInterpolator());
            //监听动画执行过程
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    //得到动画执行过程中的半径
                    mCurrentHoleRadius = (float) animation.getAnimatedValue();
                    //使onDraw()方法重新被调用
                    invalidate();
                }
            });

            //使动画执行
            mValueAnimator.start();
        }

        @Override
        void drawState(Canvas canvas) {
            drawBackground(canvas);
        }
    }

    /**
     * 绘制6个小球
     *
     * @param canvas
     */
    private void drawCircles(Canvas canvas) {
        //首先得到6个小球之间的角度
        float rotateAngle = (float) (Math.PI * 2 / mCircleColors.length);
        //开始小球的绘制
        for (int i = 0; i < mCircleColors.length; i++) {
            //每个小球的坐标：半径*cos值+圆心x坐标，半径*sin值+圆心y坐标
            float angle = i * rotateAngle + mCurrentRotateAngle;//角度
            float cx = (float) (Math.cos(angle) * mCurrentRotateRadius + mCenterX);//x坐标
            float cy = (float) (Math.sin(angle) * mCurrentRotateRadius + mCenterY);//y坐标

            //开始小球的绘制，传的参数是每个小球的颜色，即颜色数组
            mPaint.setColor(Color.parseColor("#fff"));
            Log.e("TAG_ME", "----");
            canvas.drawCircle(cx, cy, mCircleRadius, mPaint);
        }

    }

    /**
     * 绘制背景
     */
    private void drawBackground(Canvas canvas) {
        //第三种动画
        if (mCurrentHoleRadius > 0) {
            //绘制一个空心圆
            float strokeWidth = mDistance - mCurrentHoleRadius;
            float radius = strokeWidth / 2 + mCurrentHoleRadius; //真实半径
            mHolePaint.setStrokeWidth(strokeWidth);
            canvas.drawCircle(mCenterX, mCenterY, radius, mHolePaint);
        } else {
            //绘制开局的白色背景
            canvas.drawColor(mBackgroundColor);
        }

    }

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mHolePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHolePaint.setStyle(Paint.Style.STROKE);
        mHolePaint.setColor(mBackgroundColor);

        //颜色数组，取得是array目录下的颜色
        mCircleColors = context.getResources().getIntArray(R.array.splash_circle_colors);
    }
}
